package com.youkeyi.ddy.cloud.application.business.crm.recipient.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.recipient.BusinessVaccineRecipientCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.recipient.dto.BusinessVaccineRecipientListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.recipient.dto.BusinessVaccineRecipientListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.recipient.param.BusinessVaccineRecipientCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.recipient.param.BusinessVaccineRecipientDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.recipient.param.BusinessVaccineRecipientListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.recipient.param.BusinessVaccineRecipientUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.CrmVaccineRecipientCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.dto.CrmVaccineRecipientListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.param.CrmVaccineRecipientCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.param.CrmVaccineRecipientDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.param.CrmVaccineRecipientQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.recipient.param.CrmVaccineRecipientUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessVaccineRecipientController
 *
 * @Author liuxiawang
 * @Date 2024/4/19 10:58 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessVaccineRecipientController implements BusinessVaccineRecipientCloudService {
    
    @Autowired
    private CrmVaccineRecipientCloudService crmVaccineRecipientCloudService;

    @Override
    public BusinessVaccineRecipientListCloudDto list(BusinessVaccineRecipientListCloudParam param) {
        BusinessVaccineRecipientListCloudDto dto = new BusinessVaccineRecipientListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmVaccineRecipientQueryCloudParam vaccineRecipientQueryCloudParam = new CrmVaccineRecipientQueryCloudParam();
        vaccineRecipientQueryCloudParam.setVaccineRecipientPrimaryKeys(param.getVaccineRecipientPrimaryKeys());
        vaccineRecipientQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        vaccineRecipientQueryCloudParam.setCustomerKeys(param.getCustomerKeys());
        vaccineRecipientQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        vaccineRecipientQueryCloudParam.setIdCardFuzzy(param.getIdCardFuzzy());
        vaccineRecipientQueryCloudParam.setPageSize(param.getPageSize());
        vaccineRecipientQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmVaccineRecipientListCloudDto vaccineRecipientDtos = crmVaccineRecipientCloudService.query(vaccineRecipientQueryCloudParam);
        if (null != vaccineRecipientDtos && CollectionUtils.isNotEmpty(vaccineRecipientDtos.getRows())) {
            List<BusinessVaccineRecipientListItemCloudDto> items = new ArrayList<>();
            vaccineRecipientDtos.getRows().forEach(vaccineRecipient -> {
                BusinessVaccineRecipientListItemCloudDto item = new BusinessVaccineRecipientListItemCloudDto();
                item.setVaccineRecipientPrimaryKey(vaccineRecipient.getVaccineRecipientPrimaryKey());
                item.setCorpKey(vaccineRecipient.getCorpKey());
                item.setCustomerKey(vaccineRecipient.getCustomerKey());
                item.setCustomerName(vaccineRecipient.getCustomerName());
                item.setName(vaccineRecipient.getName());
                item.setSex(vaccineRecipient.getSex());
                item.setBirthday(vaccineRecipient.getBirthday());
                item.setIdCard(vaccineRecipient.getIdCard());
                item.setRelationship(vaccineRecipient.getRelationship());
                item.setRemark(vaccineRecipient.getRemark());
                item.setIsDeleted(vaccineRecipient.getIsDeleted());
                item.setIsDeletedName(vaccineRecipient.getIsDeletedName());
                item.setCreateUserKey(vaccineRecipient.getCreateUserKey());
                item.setCreateUserName(vaccineRecipient.getCreateUserName());
                item.setCreateTime(vaccineRecipient.getCreateTime());
                item.setUpdateUserKey(vaccineRecipient.getUpdateUserKey());
                item.setUpdateUserName(vaccineRecipient.getUpdateUserName());
                item.setUpdateTime(vaccineRecipient.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(vaccineRecipientDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessVaccineRecipientCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
                || StringUtils.isBlank(param.getIdCard())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmVaccineRecipientCreateCloudParam vaccineRecipientCreateCloudParam = new CrmVaccineRecipientCreateCloudParam();
        vaccineRecipientCreateCloudParam.setCorpKey(corpKey);
        vaccineRecipientCreateCloudParam.setCustomerKey(param.getCustomerKey());
        vaccineRecipientCreateCloudParam.setCustomerName(param.getCustomerName());
        vaccineRecipientCreateCloudParam.setName(param.getName());
        vaccineRecipientCreateCloudParam.setSex(param.getSex());
        vaccineRecipientCreateCloudParam.setBirthday(param.getBirthday());
        vaccineRecipientCreateCloudParam.setIdCard(param.getIdCard());
        vaccineRecipientCreateCloudParam.setRelationship(param.getRelationship());
        vaccineRecipientCreateCloudParam.setRemark(param.getRemark());
        vaccineRecipientCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        vaccineRecipientCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return crmVaccineRecipientCloudService.create(vaccineRecipientCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessVaccineRecipientUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getVaccineRecipientPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmVaccineRecipientUpdateCloudParam vaccineRecipientUpdateCloudParam = new CrmVaccineRecipientUpdateCloudParam();
        vaccineRecipientUpdateCloudParam.setVaccineRecipientPrimaryKey(param.getVaccineRecipientPrimaryKey());
        vaccineRecipientUpdateCloudParam.setCustomerKey(param.getCustomerKey());
        vaccineRecipientUpdateCloudParam.setCustomerName(param.getCustomerName());
        vaccineRecipientUpdateCloudParam.setName(param.getName());
        vaccineRecipientUpdateCloudParam.setSex(param.getSex());
        vaccineRecipientUpdateCloudParam.setBirthday(param.getBirthday());
        vaccineRecipientUpdateCloudParam.setIdCard(param.getIdCard());
        vaccineRecipientUpdateCloudParam.setRelationship(param.getRelationship());
        vaccineRecipientUpdateCloudParam.setRemark(param.getRemark());
        vaccineRecipientUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        vaccineRecipientUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmVaccineRecipientCloudService.update(vaccineRecipientUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessVaccineRecipientDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getVaccineRecipientPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmVaccineRecipientDeleteCloudParam vaccineRecipientDeleteCloudParam = new CrmVaccineRecipientDeleteCloudParam();
        vaccineRecipientDeleteCloudParam.setVaccineRecipientPrimaryKeys(param.getVaccineRecipientPrimaryKeys());
        vaccineRecipientDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        vaccineRecipientDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmVaccineRecipientCloudService.delete(vaccineRecipientDeleteCloudParam);
    }
}
